Imports System.Windows.Forms
Imports Vintasoft.Imaging.Codecs
Imports Vintasoft.Imaging.Codecs.Gif

Namespace Imaging.Codecs.Dialogs
	Public Partial Class GifEncoderSettingsForm
		Inherits Form

		#Region "Constructors"

		Public Sub New()
			InitializeComponent()

			createPageMethodComboBox.Items.Add(CreatePageMethod.UseGlobalOrLocalPalette)
			createPageMethodComboBox.Items.Add(CreatePageMethod.UseOnlyGlobalPalette)
		End Sub

		#End Region



		#Region "Properties"

		''' <summary>
		''' Determines that existing document should be append.
		''' </summary>
		Public Property AppendExistingDocument() As Boolean
			Get
				Return appendCheckBox.Checked
			End Get
			Set
				appendCheckBox.Checked = value
			End Set
		End Property

		''' <summary>
		''' Gets or sets a value indicating whether <see cref="AppendExistingDocument"/> is enabled.
		''' </summary>
		Public Property AppendExistingDocumentEnabled() As Boolean
			Get
				Return appendCheckBox.Enabled
			End Get
			Set
				appendCheckBox.Enabled = value
			End Set
		End Property


		Private _encoderSettings As GifEncoderSettings
		''' <summary>
		''' Gets or sets a GIF encoder settings.
		''' </summary>
		Public Property EncoderSettings() As GifEncoderSettings
			Get
				Return _encoderSettings
			End Get
			Set
				If value Is Nothing Then
					Throw New ArgumentOutOfRangeException()
				End If
				If _encoderSettings IsNot value Then
					_encoderSettings = value
					ShowEncoderSettings()
				End If
			End Set
		End Property

		Private Property ImagesAlign() As PageAlignMode
			Get
				If centerPositionRadioButton.Checked Then
					Return PageAlignMode.Center
				End If
				If leftTopPositionRadioButton.Checked Then
					Return PageAlignMode.LeftTop
				End If
				If topPositionRadioButton.Checked Then
					Return PageAlignMode.Top
				End If
				If rightTopPositionRadioButton.Checked Then
					Return PageAlignMode.RightTop
				End If
				If rightPositionRadioButton.Checked Then
					Return PageAlignMode.Right
				End If
				If rightBottomPositionRadioButton.Checked Then
					Return PageAlignMode.RightButtom
				End If
				If bottomPositionRadioButton.Checked Then
					Return PageAlignMode.Bottom
				End If
				If letfBottomPositionRadioButton.Checked Then
					Return PageAlignMode.LeftBottom
				End If
				If leftPositionRadioButton.Checked Then
					Return PageAlignMode.Left
				End If
				Return PageAlignMode.LeftTop
			End Get
			Set
				Select Case value
					Case PageAlignMode.Center
						centerPositionRadioButton.Checked = True
						Exit Select
					Case PageAlignMode.LeftTop
						leftTopPositionRadioButton.Checked = True
						Exit Select
					Case PageAlignMode.Top
						topPositionRadioButton.Checked = True
						Exit Select
					Case PageAlignMode.RightTop
						rightTopPositionRadioButton.Checked = True
						Exit Select
					Case PageAlignMode.Right
						rightPositionRadioButton.Checked = True
						Exit Select
					Case PageAlignMode.RightButtom
						rightBottomPositionRadioButton.Checked = True
						Exit Select
					Case PageAlignMode.Bottom
						bottomPositionRadioButton.Checked = True
						Exit Select
					Case PageAlignMode.LeftBottom
						letfBottomPositionRadioButton.Checked = True
						Exit Select
					Case PageAlignMode.Left
						leftPositionRadioButton.Checked = True
						Exit Select
				End Select
			End Set
		End Property

		#End Region



		#Region "Methods"

		Protected Overrides Sub OnLoad(e As EventArgs)
			MyBase.OnLoad(e)

			If EncoderSettings Is Nothing Then
				EncoderSettings = New GifEncoderSettings()
			End If
		End Sub

		Private Sub ShowEncoderSettings()
			createPageMethodComboBox.SelectedItem = _encoderSettings.CreatePageMethod

			infiniteIterationsCheckBox.Checked = _encoderSettings.InfiniteAnimation
			animationCyclesNumericUpDown.Value = _encoderSettings.NumberOfAnimationCycles

			animationDelayNumericUpDown.Value = _encoderSettings.AnimationDelay

			autoSizeCheckBox.Checked = _encoderSettings.LogicalScreenWidth = 0 AndAlso _encoderSettings.LogicalScreenHeight = 0
			logicalScreenWidthNumericUpDown.Value = _encoderSettings.LogicalScreenWidth
			logicalScreenHeightNumericUpDown.Value = _encoderSettings.LogicalScreenHeight

			ImagesAlign = _encoderSettings.PageAlign
		End Sub

		Private Sub SetEncoderSettings()
			_encoderSettings.CreatePageMethod = DirectCast(createPageMethodComboBox.SelectedItem, CreatePageMethod)

			_encoderSettings.NumberOfAnimationCycles = CInt(Math.Truncate(animationCyclesNumericUpDown.Value))
			_encoderSettings.InfiniteAnimation = infiniteIterationsCheckBox.Checked

			_encoderSettings.AnimationDelay = CInt(Math.Truncate(animationDelayNumericUpDown.Value))

			_encoderSettings.LogicalScreenWidth = CInt(Math.Truncate(logicalScreenWidthNumericUpDown.Value))
			_encoderSettings.LogicalScreenHeight = CInt(Math.Truncate(logicalScreenHeightNumericUpDown.Value))

			_encoderSettings.PageAlign = ImagesAlign
		End Sub

		Private Sub autoSizeCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			If autoSizeCheckBox.Checked Then
				logicalScreenWidthNumericUpDown.Enabled = False
				logicalScreenHeightNumericUpDown.Enabled = False
				logicalScreenWidthNumericUpDown.Value = 0
				logicalScreenHeightNumericUpDown.Value = 0
			Else
				logicalScreenWidthNumericUpDown.Enabled = True
				logicalScreenHeightNumericUpDown.Enabled = True
			End If
		End Sub

		Private Sub infiniteIterationsCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			If infiniteIterationsCheckBox.Checked Then
				animationCyclesNumericUpDown.Enabled = False
				animationCyclesNumericUpDown.Value = 0
			Else
				animationCyclesNumericUpDown.Enabled = True
			End If
		End Sub


		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			SetEncoderSettings()

			DialogResult = DialogResult.OK
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		#End Region


	End Class
End Namespace
